/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.bean.OrderParameterCarrier;
import com.minierp.bean.QueryParameterCarrier;
import com.minierp.bean.QueryResult;
import com.minierp.dao.QueryDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class QueryDaoImpl extends HibernateDaoSupport implements QueryDao {
	public QueryResult doQuery(int pageIndex, int pageSize,
			QueryParameterCarrier[] qParas, OrderParameterCarrier[] oParas,
			String[] gParas, Map shortNameMap, String[] retObjs,
			String[] extCountObjs) throws Exception {
		QueryResult qr = new QueryResult();
		Session sess = null;

		try {
			sess = this.getSession();
			String[] countObjs;
			if (extCountObjs != null && extCountObjs.length > 0) {
				countObjs = new String[extCountObjs.length + 1];

				for (int sql = 0; sql < extCountObjs.length; ++sql) {
					countObjs[sql] = extCountObjs[sql];
				}

				countObjs[extCountObjs.length] = "count(*)";
			} else {
				countObjs = new String[]{"count(*)"};
			}

			String arg22 = EncCommonHelper.buildNamedSQL(qParas,
					(OrderParameterCarrier[]) null, gParas, shortNameMap,
					countObjs);
			Query q = sess.createQuery(arg22);
			PageHelper.setQueryValue(q, qParas);
			List totalPage;
			int previewIndex;
			int arg25;
			if (gParas != null && gParas.length > 0) {
				if (extCountObjs != null && extCountObjs.length > 0) {
					totalPage = q.list();
					Object[] arg24 = new Object[countObjs.length];
					if (totalPage != null && !totalPage.isEmpty()) {
						arg25 = 0;

						while (true) {
							if (arg25 >= totalPage.size()) {
								arg24[arg24.length - 1] = String
										.valueOf(totalPage.size());
								break;
							}

							Object[] arg26 = (Object[]) totalPage.get(arg25);

							for (int j = 0; j < arg26.length - 1; ++j) {
								BigDecimal bd = new BigDecimal(
										arg26[j].toString());
								if (arg24[j] == null) {
									arg24[j] = bd;
								} else {
									arg24[j] = bd.add((BigDecimal) arg24[j]);
								}
							}

							++arg25;
						}
					}

					for (arg25 = 0; arg25 < arg24.length; ++arg25) {
						if (arg24[arg25] == null) {
							arg24[arg25] = new BigDecimal(0);
						}
					}

					qr.setExtCount(arg24);
				}

				qr.setTotalCount(q.list().size());
			} else if (extCountObjs != null && extCountObjs.length > 0) {
				totalPage = q.list();
				previewIndex = 0;
				Object[] nextIndex = new Object[countObjs.length];
				int i;
				if (totalPage != null && !totalPage.isEmpty()
						&& totalPage.get(0) != null) {
					nextIndex = (Object[]) totalPage.get(0);

					for (i = 0; i < nextIndex.length; ++i) {
						if (nextIndex[i] == null) {
							nextIndex[i] = new BigDecimal(0);
						} else if (!(nextIndex[i] instanceof BigDecimal)) {
							nextIndex[i] = new BigDecimal(
									nextIndex[i].toString());
						}
					}

					previewIndex = ((BigDecimal) nextIndex[nextIndex.length - 1])
							.intValue();
				}

				for (i = 0; i < nextIndex.length; ++i) {
					if (nextIndex[i] == null) {
						nextIndex[i] = new BigDecimal(0);
					}
				}

				qr.setExtCount(nextIndex);
				qr.setTotalCount(previewIndex);
			} else {
				qr.setTotalCount(Integer.parseInt(q.list().get(0).toString()));
			}

			int arg23 = qr.getTotalCount() / pageSize;
			if (qr.getTotalCount() % pageSize != 0) {
				++arg23;
			}

			if (pageIndex <= 0) {
				pageIndex = 1;
			}

			if (pageIndex > arg23) {
				pageIndex = arg23;
			}

			previewIndex = 0;
			if (pageIndex > 1) {
				previewIndex = pageIndex - 1;
			}

			arg25 = 0;
			if (pageIndex < arg23) {
				arg25 = pageIndex + 1;
			}

			qr.setPageCount(arg23);
			qr.setPageSize(pageSize);
			qr.setPageIndex(pageIndex);
			qr.setPreviewIndex(previewIndex);
			qr.setNextIndex(arg25);
			arg22 = EncCommonHelper.buildNamedSQL(qParas, oParas, gParas,
					shortNameMap, retObjs);
			q = sess.createQuery(arg22);
			PageHelper.setQueryValue(q, qParas);
			q.setFirstResult((pageIndex - 1) * pageSize);
			q.setMaxResults(pageSize);
			qr.setList(q.list());
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return qr;
	}

	public QueryResult doQuery(QueryParameterCarrier[] qParas,
			OrderParameterCarrier[] oParas, String[] gParas, Map shortNameMap,
			String[] retObjs, String[] countObjs) throws Exception {
		QueryResult qr = new QueryResult();
		Session sess = null;

		try {
			sess = this.getSession();
			String sql;
			Query q;
			if (countObjs != null && countObjs.length > 0) {
				sql = EncCommonHelper.buildNamedSQL(qParas,
						(OrderParameterCarrier[]) null, gParas, shortNameMap,
						countObjs);
				q = sess.createQuery(sql);
				PageHelper.setQueryValue(q, qParas);
				List list = q.list();
				Object[] objs = (Object[]) null;
				if (list != null && !list.isEmpty()) {
					if (countObjs.length == 1) {
						objs = new Object[]{list.get(0)};
					} else {
						objs = (Object[]) list.get(0);
					}
				}

				if (objs == null) {
					objs = new Object[countObjs.length];
				}

				for (int i = 0; i < objs.length; ++i) {
					if (objs[i] == null) {
						objs[i] = new BigDecimal(0);
					} else if (!(objs[i] instanceof BigDecimal)) {
						objs[i] = new BigDecimal(objs[i].toString());
					}
				}

				qr.setExtCount(objs);
			}

			sql = EncCommonHelper.buildNamedSQL(qParas, oParas, gParas,
					shortNameMap, retObjs);
			q = sess.createQuery(sql);
			PageHelper.setQueryValue(q, qParas);
			qr.setList(q.list());
			qr.setTotalCount(qr.getList().size());
			qr.setPageSize(qr.getList().size());
			qr.setPageCount(1);
			qr.setPageIndex(1);
			qr.setPreviewIndex(0);
			qr.setNextIndex(0);
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return qr;
	}
}